<template>
  <div>
    <div>商品类型</div>
    <div>
      <el-button type="primary" @click="handLeAdd()">添加类型</el-button>
    </div>
    <el-table
      :data="typeTree"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="name" label="名称" sortable />
      <el-table-column prop="typeStatus" label="状态">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-tag type="success" v-if="scope.row.typeStatus == 0"
              >显示</el-tag
            >
            <el-tag type="danger" v-if="scope.row.typeStatus == 1">隐藏</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="typeTop" label="排序" sortable />
      <el-table-column prop="typeImage" label="类型图片">
        <template #default="scope">
          <el-avatar shape="square" :size="100" :src="scope.row.typeImage" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" sortable />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handLeEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="success" @click="handleDelet(scope.row)"
            >删除</el-button
          >
          <el-button
            type="info"
            v-if="scope.row.typeStatus == 0"
            @click="handleUpdate(scope.row.id, 1)"
            >隐藏</el-button
          >
          <el-button
            v-if="scope.row.typeStatus == 1"
            type="warning"
            @click="handleUpdate(scope.row.id, 0)"
            >显示</el-button
          >
          <el-button type="warning" @click="onAddType(scope.row)"
            >添加</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
      <el-form :model="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.typeName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:10010/shop/type/uploadFile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="headers"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="分类排序" :label-width="formLabelWidth">
          <el-input v-model="form.typeTop" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类状态" :label-width="formLabelWidth">
          <div class="mb-2 ml-4">
            <el-radio-group v-model="form.typeStatus">
              <el-radio :value="0" size="large">显示</el-radio>
              <el-radio :value="1" size="large">隐藏</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button
            type="primary"
            v-if="title == '添加类型'"
            @click="addTreeFrom()"
          >
            添加
          </el-button>
          <el-button
            type="primary"
            v-if="title != '添加类型'"
            @click="onSubmitAddType()"
          >
            修改
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { Addtypee, updateType } from "../api/api";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const title = ref("添加类型");
import type { UploadProps } from "element-plus";
// 添加二级目录
const onAddType = (row: any) => {
  restFrom();
  form.value.typePid = row.id;
  dialogFormVisible.value = true;
};

const imageUrl = ref("");
/**
 * 图片上传回显
 */
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
  // 用于添加图片
  form.value.typeImage = imageUrl.value;
};

const headers = {
  token: sessionStorage.getItem("token"),
};

import {
  getTypeTree,
  setTypeStatus,
  deleteTypeById,
  uploadFile,
} from "../api/api";
const typeTree = ref([]);

// 二级菜单
const restFrom = () => {
  form.value = {
    createBy: "",
    createTime: "",
    typeId: null,
    typeImage: "",
    typeName: "",
    typePid: null,
    typeStatus: null,
    typeTop: null,
    updateBy: "",
    updateTime: "",
  };
  imageUrl.value = "";
};

const form = ref({
  createBy: "",
  createTime: "",
  typeId: null,
  typeImage: "",
  typeName: "",
  typePid: null,
  typeStatus: null,
  typeTop: null,
  updateBy: "",
  updateTime: "",
});

/**
 * 弹出框修改
 */
const onSubmitAddType = () => {
  form.value.typeImage = imageUrl.value;
  updateType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadTypeTree();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

/**
 * 添加
 */
const addTreeFrom = () => {
  Addtypee(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      loadTypeTree();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

import { ElMessage, ElMessageBox } from "element-plus";
import { stat } from "fs";
import { load } from "@amap/amap-jsapi-loader";
import { fa } from "element-plus/es/locales.mjs";
// 添加
const handLeAdd = () => {
  form.value.typePid = 0;
  dialogFormVisible.value = true;
  title.value = "添加类型";
};

// 修改
const handleUpdate = (typeid: number, status: number) => {
  setTypeStatus(typeid, status).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadTypeTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

// 编辑
const handLeEdit = (row: any) => {
  title.value = "编辑类型";
  Object.assign(form.value, row);
  form.value.typeName = row.name;
  form.value.typeId = row.id;
  form.value.typePid = row.parentId;
  imageUrl.value = row.typeImage;
  dialogFormVisible.value = true;
};
// 删除
const handleDelet = (row: any) => {
  ElMessageBox.confirm("确定要删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteTypeById(row.id).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadTypeTree();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

// 获取商品类型树
const loadTypeTree = () => {
  getTypeTree().then((res) => {
    console.log("typeTree", res);
    if (res.data.code == 200) {
      typeTree.value = res.data.data;
    }
  });
};

loadTypeTree();
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
